package com.pap.upload.file.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.springframework.util.StreamUtils;

import com.pap.base.util.string.StringUtilss;

public class ZipUtil {

	/**
	 * 功能： <br/>
	 *
	 */
	public static ByteArrayInputStream zip(Map<File, String> files) throws IOException {
		return zip(files, StandardCharsets.UTF_8);
	}

	/**
	 * 功能：zip压缩 <br/>
	 *
	 */
	public static ByteArrayInputStream zip(Map<File, String> files, Charset charset) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		zip(files, baos, charset);

		return new ByteArrayInputStream(baos.toByteArray());
	}

	/**
	 * 功能： <br/>
	 *
	 */
	public static void zip(Map<File, String> files, OutputStream os) throws IOException {
		zip(files, os, StandardCharsets.UTF_8);
	}

	/**
	 * 功能：zip压缩 <br/>
	 *
	 */
	public static void zip(Map<File, String> files, OutputStream os, Charset charset) throws IOException {
		// 创建zip流
		try (ZipOutputStream zos = new ZipOutputStream(os, charset)) {
			// 依次处理每个文件
			for (Map.Entry<File, String> entry : files.entrySet()) {
				File file = entry.getKey();
				String name = entry.getValue();
				if (StringUtilss.isEmpty(name)) {
					name = file.getName();
				}
				zos.putNextEntry(new ZipEntry(name));

				try (InputStream is = new FileInputStream(file)) {
					// 将文件内容写入到zip流中
					StreamUtils.copy(is, zos);
				}
			}
		}
	}
	
	/**
	 * 压缩多个文件
	 * @param files
	 * @param zipFilePathStr
	 * @throws IOException
	 */
	public static void generatorZipFile(Map<File, String> files, String zipFilePathStr) throws IOException {
		File zipfile = new File(zipFilePathStr);
		byte[] buf = new byte[1024];  
        try {  
            //ZipOutputStream类：完成文件或文件夹的压缩  
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));  
            for (Map.Entry<File, String> entry : files.entrySet()) {
                FileInputStream in = new FileInputStream(entry.getKey());  
                out.putNextEntry(new ZipEntry(entry.getValue()));  
                int len;  
                while ((len = in.read(buf)) > 0) {  
                    out.write(buf, 0, len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}

	
	/**
	 * 压缩多个文件，生成zip压缩文件
	 * @param files		获取的多个文件
	 * @param zipfile	压缩生成的文件
	 * @throws IOException
	 */
	public static void generatorZipFile(Map<File, String> files, File zipfile) throws IOException {
		byte[] buf = new byte[1024];  
        try {  
            //ZipOutputStream类：完成文件或文件夹的压缩  
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));  
            for (Map.Entry<File, String> entry : files.entrySet()) {
                FileInputStream in = new FileInputStream(entry.getKey());  
                out.putNextEntry(new ZipEntry(entry.getValue()));  
                int len;  
                while ((len = in.read(buf)) > 0) {  
                    out.write(buf, 0, len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}

}
